package KK.Controls.Skin
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class ButtonSheet implements ISkinSheet
	{
		static private const MAX_WIDTH:int=3;
		
		private var _area:Vector.<Rectangle>;
		private var _targetRect:Vector.<Rectangle>;
		
		private var _tileW:int;
		private var _tileH:int;
		
		private var offset:Point;
		public function ButtonSheet(width:int,height:int)
		{
			//TODO: implement function
			_area=new Vector.<Rectangle>(MAX_WIDTH,true);
			_tileW=width/3;
			_tileH=height;
			init();
		}
		private function init():void{
			offset=new Point();
			calcSheet();
		}
		
		/**
		 * 计算片段  
		 * 
		 */
		public function calcSheet():void{
			var i:int=0;
			for(i=0;i<MAX_WIDTH;i++){
				_area[i]=new Rectangle(i*_tileW+offset.x,0+offset.y,_tileW,_tileH);
			}
		
		}
		
		/**
		 * 设置源偏移 
		 * @param value
		 * 
		 */
		public function setOffset(value:Point):void{
			offset=value;
			calcSheet();
		}
		
		public function get Area():Vector.<Rectangle>
		{
			//TODO: implement function
			return _area;
		}
		
		/**
		 * 设置大小  
		 * @param size
		 * @param desPoint
		 * 
		 */
		public function setSize(size:Point,desPoint:Point):void{
			_targetRect=calcTarget(size,_area,desPoint);
		}
		
		/**
		 * 目标区域  
		 * @return 
		 * 
		 */
		public function get targetRect():Vector.<Rectangle>{
			return _targetRect;
		}
		
		
		/**
		 * 赋值一份数据  
		 * @return 
		 * 
		 */
		public function clone():ISkinSheet{
			var result:ButtonSheet=new ButtonSheet(0,0);
			result._tileH=this._tileH;
			result._tileW=this._tileW;
			result.offset=offset.clone();
			
			var i:int=0;
			var len:int=0;
			if(_area){
				len=_area.length;
				result._area=new Vector.<Rectangle>(MAX_WIDTH,true);
				
				for(i=0;i<len;i++){
					result._area[i]=_area[i].clone();
				}
			}
			
			if(_targetRect){
				len=_targetRect.length;
				result._targetRect=new Vector.<Rectangle>(MAX_WIDTH,true);
				
				for(i=0;i<len;i++){
					result._targetRect[i]=_targetRect[i].clone();
				}
			}
			return result;
		}
		
		/**
		 * 计算目标区域  
		 * @param size
		 * @param Area
		 * @param desPoint //对于source偏移多少 
		 * 
		 */
		private function calcTarget(size:Point,Area:Vector.<Rectangle>,desPoint:Point):Vector.<Rectangle>{
			var result:Vector.<Rectangle>=new Vector.<Rectangle>(MAX_WIDTH,true);
			
			result[0]=new Rectangle(desPoint.x,desPoint.y,Area[0].width,Area[0].height);
			result[2]=new Rectangle(desPoint.x+size.x-Area[2].width,desPoint.y,Area[2].width,Area[2].height);
			result[1]=new Rectangle(result[0].right,desPoint.y,size.x-Area[0].width-Area[2].width,Area[1].height);
			
			return result;
		}
		
		
		
	}
}